/* Copyright 2015 Esri
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.esri.arcgis.android.sample.runtimegeodb;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import com.esri.android.map.FeatureLayer;
import com.esri.android.map.MapView;
import com.esri.core.ags.FeatureServiceInfo;
import com.esri.core.geodatabase.Geodatabase;
import com.esri.core.geodatabase.GeodatabaseFeatureTable;
import com.esri.core.map.CallbackListener;
import com.esri.core.tasks.geodatabase.GenerateGeodatabaseParameters;
import com.esri.core.tasks.geodatabase.GeodatabaseStatusCallback;
import com.esri.core.tasks.geodatabase.GeodatabaseStatusInfo;
import com.esri.core.tasks.geodatabase.GeodatabaseSyncTask;

import java.io.File;
import java.io.FileNotFoundException;

public class CreateRuntimeGeodatabaseActivity extends Activity {

	static MapView				mMapView;

	String 						featureServerURL;
	static GeodatabaseSyncTask 	gdbSyncTask;

	static ProgressDialog 		mProgressDialog;

	private static File 		demoDataFile;
	private static String 		offlineDataSDCardDirName;
	private static String 		filename;

	static String 				localGdbFilePath;
	
	protected static final String 	TAG = "CRGdb";
	protected static String 		OFFLINE_FILE_EXTENSION = ".geodatabase";
	
	private static Context 			mContext;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		CreateRuntimeGeodatabaseActivity.setContext(this);

		demoDataFile 				= Environment.getExternalStorageDirectory();
		offlineDataSDCardDirName 	= "ArcGIS/samples/CRGdb";
		filename 					= "wildfire";

		mMapView 		 = (MapView) findViewById(R.id.map);

		featureServerURL = "http://services.arcgis.com/P3ePLMYs2RVChkJx/arcgis/rest/services/Wildfire/FeatureServer";
		
		mProgressDialog = new ProgressDialog(CreateRuntimeGeodatabaseActivity.this);
		mProgressDialog.setTitle("Create local runtime geodatabase");
		
		mMapView.setEsriLogoVisible(true);
		mMapView.enableWrapAround(true);

	}
	
	// methods to ensure context is available when updating the progress dialog
	public static Context getContext(){
		return mContext;
	}
	
	public static void setContext(Context context){
		mContext = context;
	}

	// 生成 本地文件存储路径
	static String createGeodatabaseFilePath() {
		return demoDataFile.getAbsolutePath() + File.separator + offlineDataSDCardDirName + File.separator + filename + OFFLINE_FILE_EXTENSION;
	}

	// 菜单
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case R.id.action_download:
			// 下载
			downloadData(featureServerURL);

			return true;
		default:
			return super.onOptionsItemSelected(item);
		}

	}

	/** Create the GeodatabaseTask from the feature service URL w/o credentials. **/
	private void downloadData(String url) {

		mProgressDialog.show();

		gdbSyncTask = new GeodatabaseSyncTask(url, null);
		gdbSyncTask.fetchFeatureServiceInfo(new CallbackListener<FeatureServiceInfo>() {

			@Override
			public void onError(Throwable arg0) {
				Log.e(TAG, "Error fetching FeatureServiceInfo");
			}

			@Override
			public void onCallback(FeatureServiceInfo fsInfo) {
				if (fsInfo.isSyncEnabled()) {
					createGeodatabase(fsInfo);
				}
			}

		});

	}

	// 创建本地地理数据库
	private static void createGeodatabase(FeatureServiceInfo featureServerInfo) {
		// set up the parameters to generate a geodatabase
		GenerateGeodatabaseParameters params = new GenerateGeodatabaseParameters(featureServerInfo, mMapView.getExtent(), mMapView.getSpatialReference());

		// a callback which fires when the task has completed or failed.
		CallbackListener<String> gdbResponseCallback = new CallbackListener<String>() {
			@Override
			public void onError(final Throwable e) {
				mProgressDialog.dismiss();
			}

			@Override
			public void onCallback(String path) {
				mProgressDialog.dismiss();
				// update map with local feature layer from geodatabase
				updateFeatureLayer(path);
			}
		};

		// a callback which updates when the status of the task changes
		GeodatabaseStatusCallback statusCallback = new GeodatabaseStatusCallback() {
			@Override
			public void statusUpdated(final GeodatabaseStatusInfo status) {
				// get current status
				String progress = status.getStatus().toString();
				Log.i("andli","当前状态: "+progress);

				Context context = CreateRuntimeGeodatabaseActivity.getContext();
				CreateRuntimeGeodatabaseActivity activity = (CreateRuntimeGeodatabaseActivity) context;
				showProgressBar(activity, progress);

			}
		};

		// create the fully qualified path for geodatabase file
		localGdbFilePath = createGeodatabaseFilePath();

		// get geodatabase based on params
		submitTask(params, localGdbFilePath, statusCallback, gdbResponseCallback);
	}

	// Request database, poll server to get status, and download the file
	private static void submitTask(GenerateGeodatabaseParameters params, String file, GeodatabaseStatusCallback statusCallback, CallbackListener<String> gdbResponseCallback) {
		// submit task
		gdbSyncTask.generateGeodatabase(params, file, false, statusCallback, gdbResponseCallback);
	}

	// 从本地地理数据库 获取属性图层 添加到mapview
	private static void updateFeatureLayer(String featureLayerPath) {
		// 得到本地数据库
		Geodatabase localGdb = null;
		try {
			localGdb = new Geodatabase(featureLayerPath);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}

		// Geodatabase contains GdbFeatureTables representing attribute data and/or spatial data.
		// If GdbFeatureTable has geometry add it to the MapView as a Feature Layer
		if (localGdb != null) {
			for (GeodatabaseFeatureTable gdbFeatureTable : localGdb.getGeodatabaseTables()) {
				// 如果有几何对象，添加到图层
				if (gdbFeatureTable.hasGeometry()){
					mMapView.addLayer(new FeatureLayer(gdbFeatureTable));
				}
			}
		}

	}
	
	private static void showProgressBar(final CreateRuntimeGeodatabaseActivity activity, final String message){
		activity.runOnUiThread(new Runnable(){

			@Override
			public void run() {
				mProgressDialog.setMessage(message);
			}
			
		});
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		super.onPause();
		mMapView.pause();
	}

	@Override
	protected void onResume() {
		super.onResume();
		mMapView.unpause();
	}

}